<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="title">
      <el-input v-model="dataForm.title" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="description">
      <el-input v-model="dataForm.description" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="contents">
      <el-input v-model="dataForm.contents" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="thumb">
      <el-input v-model="dataForm.thumb" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="cateid">
      <el-input v-model="dataForm.cateid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="created">
      <el-input v-model="dataForm.created" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="click">
      <el-input v-model="dataForm.click" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="province">
      <el-input v-model="dataForm.province" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="city">
      <el-input v-model="dataForm.city" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="area">
      <el-input v-model="dataForm.area" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="userid">
      <el-input v-model="dataForm.userid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="address">
      <el-input v-model="dataForm.address" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="zan">
      <el-input v-model="dataForm.zan" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="fav">
      <el-input v-model="dataForm.fav" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="attachfile">
      <el-input v-model="dataForm.attachfile" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          title: '',
          description: '',
          contents: '',
          thumb: '',
          cateid: '',
          created: '',
          click: '',
          province: '',
          city: '',
          area: '',
          userid: '',
          address: '',
          zan: '',
          fav: '',
          attachfile: ''
        },
        dataRule: {
          title: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          contents: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          thumb: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          cateid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          created: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          click: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          province: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          userid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          zan: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          fav: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          attachfile: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/news/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.news.title
                this.dataForm.description = data.news.description
                this.dataForm.contents = data.news.contents
                this.dataForm.thumb = data.news.thumb
                this.dataForm.cateid = data.news.cateid
                this.dataForm.created = data.news.created
                this.dataForm.click = data.news.click
                this.dataForm.province = data.news.province
                this.dataForm.city = data.news.city
                this.dataForm.area = data.news.area
                this.dataForm.userid = data.news.userid
                this.dataForm.address = data.news.address
                this.dataForm.zan = data.news.zan
                this.dataForm.fav = data.news.fav
                this.dataForm.attachfile = data.news.attachfile
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/news/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'description': this.dataForm.description,
                'contents': this.dataForm.contents,
                'thumb': this.dataForm.thumb,
                'cateid': this.dataForm.cateid,
                'created': this.dataForm.created,
                'click': this.dataForm.click,
                'province': this.dataForm.province,
                'city': this.dataForm.city,
                'area': this.dataForm.area,
                'userid': this.dataForm.userid,
                'address': this.dataForm.address,
                'zan': this.dataForm.zan,
                'fav': this.dataForm.fav,
                'attachfile': this.dataForm.attachfile
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
